package com.xupt.ttms.controller;

import com.github.pagehelper.PageInfo;
import com.xupt.ttms.pojo.*;
import com.xupt.ttms.service.OrderService;
import com.xupt.ttms.service.TicketService;
import com.xupt.ttms.service.UserService;
import com.xupt.ttms.util.ThreadUtils;
import com.xupt.ttms.util.ToResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

@RestController
@Slf4j
@CrossOrigin
@RequestMapping("order")
public class OrderServlet {
    @Resource
    private Order order;
    @Resource
    private OrderService orderService;
    @Resource
    private UserService userService;
    @PostMapping("/insertOrder/{pId}")
    public Result insertOrder(@CookieValue("username") String username, @RequestParam("amount") BigDecimal amount,@RequestBody List<Seat> list , @PathVariable("pId") String pId){
        User user =  userService.getUserByUsername(username);
        order.setUserId(user.getId());
        order.setAmount(amount);
        order.setOrderNo(ToResult.getOrderNo());
        orderService.insertOrder(order,list,pId);
        return ToResult.getResult(order);
    }
    @PostMapping("/payOrder")
    public Result payOrder(@RequestBody Order order1){
        return ToResult.getResult(orderService.payOrder(order1, ThreadUtils.getUserHolder().getUsername())>0?"支付成功":"支付失败");
    }
    @PostMapping("/getOrders")
    public Result getOrders(@RequestParam("page") int pageNum,@RequestParam("pageSize") int pageSize){
        User user = ThreadUtils.getUserHolder();
        log.info("用户信息为"+user);
        PageInfo<Order> orders =  orderService.getOrders(1,5,user.getId());
        return ToResult.getResult(orders);
    }
}